package com.xintonghua.bussiness.ui.fragment;

import android.support.design.widget.TabLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.CollegeAdapter;
import com.xintonghua.bussiness.base.BaseFragment;
import com.xintonghua.bussiness.bean.CollectBean;
import com.xintonghua.bussiness.bean.CollectResponse;
import com.xintonghua.bussiness.bean.HealthCultureTypeBean;
import com.xintonghua.bussiness.ui.fragment.college.JiaMuActivity;
import com.xintonghua.bussiness.util.GlideUtils;
import com.xintonghua.bussiness.util.JumpUtils;
import com.xintonghua.bussiness.util.MyState;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 商学院
 * 作者：wanxianyi
 * 时间：2018/1/11.
 * 包名：com.xintonghua.bussiness.ui.fragment
 */

public class CollegeFragment extends BaseFragment implements XRecyclerView.LoadingListener {
    @BindView(R.id.tab_college_type)
    TabLayout tabCollegeType;
    @BindView(R.id.lv_college)
    XRecyclerView lvCollege;
    @BindView(R.id.tv_jiamubiao)
    TextView tv_jiamubiao;


    Unbinder unbinder;
    private CollegeAdapter adapter;
    List<HealthCultureTypeBean> list;
    ImageView ivBannerHead;
    private int type;
    private int page = 1;
    private List<CollectBean> collectBeans = new ArrayList<>();
    private CollectResponse response;

    @Override
    public int getLayout() {
        return R.layout.fragment_college;
    }

    @Override
    public void initUI(View view) {
        unbinder = ButterKnife.bind(this, view);
        RefreshUtils.initList(getActivity(), lvCollege);
        View head = LayoutInflater.from(getContext()).inflate(R.layout.layout_price, lvCollege, false);
        ivBannerHead = head.findViewById(R.id.iv_banner_head);
        lvCollege.addHeaderView(head);
        adapter = new CollegeAdapter(getActivity(), collectBeans);
        lvCollege.setAdapter(adapter);
        lvCollege.setLoadingListener(this);
        httpCent.healthCultureType(this, MyState.requestOne);
        tv_jiamubiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JumpUtils.jumpto(getActivity(), JiaMuActivity.class,null);
            }
        });
    }


    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                list = JSONObject.parseArray((String) data, HealthCultureTypeBean.class);
                initBanner();
                break;
            case MyState.requestTwo:
                if (page == 1) {
                    collectBeans.clear();
                }
                response = JSONObject.parseObject((String) data, CollectResponse.class);
                collectBeans.addAll(response.getList());
                adapter.notifyDataSetChanged();
                RefreshUtils.onRefresh(adapter, lvCollege);
                break;
        }
    }

    /**
     * 显示分类和对应的图片
     */
    private void initBanner() {
        if (list == null || list.size() == 0) {
            return;
        }
        type = list.get(0).getId();
        GlideUtils.load(getActivity(), ivBannerHead, list.get(0).getImg(), R.drawable.icon_price);
        find();
        for (HealthCultureTypeBean bean : list) {
            tabCollegeType.addTab(tabCollegeType.newTab().setText(bean.getName()).setTag(bean));
        }
        tabCollegeType.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                HealthCultureTypeBean bean = (HealthCultureTypeBean) tab.getTag();
                GlideUtils.load(getActivity(), ivBannerHead, bean.getImg(), R.drawable.icon_price);
                type = bean.getId();
                find();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private void find() {
        httpCent.healthCulture(page, type, this, MyState.requestTwo);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        page = 1;
        find();
    }

    @Override
    public void onLoadMore() {
        if (RefreshUtils.hasNext(response, adapter, lvCollege)) {
            page++;
            find();
        }
    }
}
